summaryrefslogtreecommitdiffstats
path: root/src/pages/article/[slug].tsx
blob: 57982e9c9aa53dff27b20ac9ddb8054aeeecf5b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import CommentsList from '@components/CommentsList/CommentsList';
import Layout from '@components/Layouts/Layout';
import PostFooter from '@components/PostFooter/PostFooter';
import PostHeader from '@components/PostHeader/PostHeader';
import { t } from '@lingui/macro';
import { fetchAllPostsSlug } from '@services/graphql/blog';
import { getPostBySlug } from '@services/graphql/post';
import { NextPageWithLayout } from '@ts/types/app';
import { ArticleProps } from '@ts/types/articles';
import { loadTranslation } from '@utils/helpers/i18n';
import { GetStaticPaths, GetStaticProps, GetStaticPropsContext } from 'next';
import { ParsedUrlQuery } from 'querystring';
import { ReactElement } from 'react';

const SingleArticle: NextPageWithLayout<ArticleProps> = ({ post }) => {
  const { author, comments, content, date, intro, subjects, thematics, title } =
    post;

  return (
    <article>
      <PostHeader
        author={author}
        date={date}
        intro={intro}
        title={title}
        thematics={thematics}
      />
      <div dangerouslySetInnerHTML={{ __html: content }}></div>
      <PostFooter subjects={subjects} />
      <section>
        <h2>{t`Comments`}</h2>
        <CommentsList comments={comments} />
      </section>
    </article>
  );
};

SingleArticle.getLayout = function getLayout(page: ReactElement) {
  return <Layout>{page}</Layout>;
};

interface PostParams extends ParsedUrlQuery {
  slug: string;
}

export const getStaticProps: GetStaticProps = async (
  context: GetStaticPropsContext
) => {
  const translation = await loadTranslation(
    context.locale!,
    process.env.NODE_ENV === 'production'
  );
  const { slug } = context.params as PostParams;
  const post = await getPostBySlug(slug);

  return {
    props: {
      post,
      translation,
    },
  };
};

export const getStaticPaths: GetStaticPaths = async () => {
  const allSlugs = await fetchAllPostsSlug();

  return {
    paths: allSlugs.map((post) => `/article/${post.slug}`),
    fallback: true,
  };
};

export default SingleArticle;